<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#include "../common/header.ftl" />
    <script src="/js/ckeditor/ckeditor.js"></script>
</head>

<script>
    $(function () {
        //富文本编辑器
        var ck = CKEDITOR.replace('editor');
        //通过攻略id查询类别
        function selectByStrategyId(strategyId, catalogId) {
            $.get("/strategyCatalog/selectByStrategyId.do", {strategyId: strategyId}, function (data) {
                console.log(data);
                //将查到的类别拼接到相应的select
                var temp = "";
                $.each(data, function (index, ele) {
                    temp += "<option value='" + ele.id + "'>" + ele.name + "</option>";
                });
                console.log(temp);
                $(temp).appendTo("#strategyCatalog");
                if (catalogId) {
                    $("#editForm select[name='catalog.id']").val(catalogId);
                }
            });
        }

        //修改按钮
        $(".inputBtn").click(function () {
            var json = $(this).data("json");
            if (json) {
                $("#editForm input[name='id']").val(json.id);
                $("#editForm input[name='title']").val(json.title);
                $("#editForm img[name='coverUrl']").attr("src", json.coverUrl);
                $("#editForm input[name='coverUrl']").val(json.coverUrl);
                $("#strategy").val(json.strategyId);
                //调方法,查询对应的目录并回显
                selectByStrategyId(json.strategyId, json.catalogId);
                $("#editForm input[name='sequence']").val(json.sequence);
                $("#editForm select[name='state']").val(json.state);
                //回显文章内容
                $.get("/strategyDetail/getContentById.do",{id:json.id},function (data) {
                    ck.setData(data.content);
                });
            } else {
                //清除图片
                $("#editForm img").attr("src", "");
                //清除攻略和目录的值
                $("#editForm input,#editForm select").val("");
                //清除目录的内容
                $("#editForm select[name='catalog.id']").html("");
                //清空编辑器内容
                ck.setData("");
            }
            $("#strategyDetailModel").modal("show");
        });
        //攻略的值改变事件
        $("#strategy").change(function () {
            //每次改变,先清空目录
            $("#editForm select[name='catalog.id']").html("");
            selectByStrategyId($("#strategy").val());
        });
        //模态框的保存按钮
        $(".submitButton").click(function () {
            //将编辑器内容放入表单中
            var data = ck.getData();
            $("#editor").val(data);
            $("#editForm").ajaxSubmit(function () {
                reload();
            });
        });

    })
</script>
<body>
<!-- 模态框（Modal） -->
<div class="modal fade" id="strategyDetailModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">新增游记推荐</h4>
            </div>
        <#--游记内容-->
            <div class="modal-body">
            </div>
            <form class="form-horizontal" action="/strategyDetail/saveOrUpdate.do" method="post" id="editForm">
                <input type="hidden" name="id">
                <div class="form-group">
                    <label for="name" class="col-sm-4 control-label">标题：</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" name="title">
                    </div>
                </div>
                <div class="form-group">
                    <label for="name" class="col-sm-4 control-label">封面：</label>
                    <div class="col-sm-6">
                        <img name="coverUrl" width="100%">
                        <input type="file" class="form-control" name="file">
                        <input type="hidden" class="form-control" name="coverUrl">
                    </div>
                </div>
                <div class="form-group">
                    <label for="sn" class="col-sm-4 control-label">所属攻略：</label>
                    <div class="col-sm-6">
                        <select class="form-control" id="strategy">
                                    <#list strategies as strategy>
                                        <option value="${strategy.id}">${strategy.title}</option>
                                    </#list>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label for="sn" class="col-sm-4 control-label">攻略类别：</label>
                    <div class="col-sm-6">
                        <select class="form-control" name="catalog.id" id="strategyCatalog">

                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label for="sn" class="col-sm-4 control-label">状态：</label>
                    <div class="col-sm-6">
                        <select class="form-control" name="state">
                            <option value="0">草稿</option>
                            <option value="1">发布</option>
                            <option value="-1">禁用</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label for="name" class="col-sm-4 control-label">序号：</label>
                    <div class="col-sm-6">
                        <input type="number" class="form-control" name="sequence">
                    </div>
                </div>
                <textarea name="content.content" id="editor" rows="10" cols="80">

                </textarea>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary submitButton">保存</button>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl" />
    <div class="row">
        <div class="col-sm-3">
            <#assign menu="strategyDetail"/>
            <#include "../common/menu.ftl" />
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">攻略文章管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/strategyDetail/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" name="keyword" value="${(qo.keyword)!}" placeholder="请输入标题">
                </div>
                <button id="btn_query" class="btn btn-success" type="submit">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>
                <button id="btn_add" class="btn btn-success inputBtn" type="button">
                    <span class="glyphicon glyphicon-plus"></span> 添加
                </button>
            </form>

            <table class="table table-striped table-hover">
                <tr>
                    <th>编号</th>
                    <th>标题</th>
                    <th>封面</th>
                    <th>发布时间</th>
                    <th>排序</th>
                    <th>攻略类别</th>
                    <th>状态</th>
                    <th></th>
                </tr>
                <#list pageInfo.list as entity>
                    <tr>
                        <td>${entity_index + 1}</td>
                        <td>${entity.title}</td>
                        <td><img src="${entity.coverUrl}" width="50px"></td>
                        <td>${(entity.releaseTime?string("yyyy-MM-dd HH:mm:ss"))!}</td>
                        <td>${entity.sequence}</td>
                        <td>${entity.catalog.name}</td>
                        <td>${entity.state}</td>
                        <td>
                            <a class="btn btn-info btn-xs btn-input inputBtn" data-json='${entity.json}'>
                                <span class="glyphicon glyphicon-pencil"></span>修改
                            </a>
                        </td>
                    </tr>
                </#list>
            </table>
            <#include "../common/page.ftl" />
        </div>
    </div>
</div>

<script>

</script>
</body>
</html>